﻿using System.Web.Mvc;
using Pay.DTO;
using Pay.IService;
using Pay.TO;

namespace Pay.Web.Areas.Admin.Controllers {
    [RouteArea("admin")]
    [RoutePrefix("product")]
    public class ProductController : AdminController {
        private IProductService _iProductService;
        private ICategoryService _iCategoryService;
        private ISkuInfoService _iSkuInfoService;
        private IMerchantService _iMerchantService;

        public ProductController(IProductService iProductService, ICategoryService iCategoryService, ISkuInfoService iSkuInfoService, IMerchantService iMerchantService) {
            _iProductService = iProductService;
            _iCategoryService = iCategoryService;
            _iSkuInfoService = iSkuInfoService;
            _iMerchantService = iMerchantService;
        }

        /// <summary>
        /// 查询商品列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public JsonResult Gets(GoodsQuery query) {
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            query.merchant_id = merchant.id;
            var model = _iProductService.Get(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 查询单个商品，包括促销活动，价格随活动价变化
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Get(long id) {
            var model = _iProductService.Get(id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 查询子类、将子类放在children中
        /// </summary>
        /// <returns></returns>
        public JsonResult Categorys(long id) {
            var model = _iCategoryService.Get(id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns></returns>
        public JsonResult GetTokenCategorys() {
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            var list = _iCategoryService.GetAll(merchant.id,1);
            return Json(ResultToJson.Json(list), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 编辑商品分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public JsonResult EditProductCategory(long id, long categoryId) {
            var result = _iProductService.EditProductCategory(id,categoryId);
            return Json(ResultToJson.Json(result >= 0 ? " 修改成功" : "修改失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取规格信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetSkuInfo(long merchant_id) {
            var model = _iSkuInfoService.Get(merchant_id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 上架/下架
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Disable(long id) {
            var result = _iProductService.UpDown(id);
            return Json(ResultToJson.Json(result >= 0 ? " 修改成功" : "修改失败", result), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        ///  商品审核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Audit(product_audit_req model) {
            var result = _iProductService.Audit(model.id, model.audit_status, model.audit_reason);
            return Json(ResultToJson.Json(result >= 0 ? " 审核成功" : "审核失败", result), JsonRequestBehavior.AllowGet);
        }
    }
}